﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using VisionCore;
using PCBase.UIControls;
using DLLPC.Utilities;

namespace COER
{
    public partial class FormChipData : Form
    {
        public FormChipData()
        {
            InitializeComponent();

            GetControls(this.Controls);
        }

        private void GetControls(Control.ControlCollection ctrls)
        {
            foreach (Control item in ctrls)
            {
                if (item is Button && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((Button)item).Click += new EventHandler(this.btn_Switch_Click);
                }

                if (item is DLLPC.PCAP.PCButton && item.Tag?.ToString().Split('|')[0] == "btn")
                {
                    ((DLLPC.PCAP.PCButton)item).OP = Kernel.OP;
                }

                if (item.Controls != null)
                {
                    GetControls(item.Controls);
                }
            }
        }

        private int modelNo;
        private ChipInfo chipData;
        private string wfID;
        public void ShowForm(int idx, ChipInfo info)
        {
            chipData = info;
            propertyGrid1.SelectedObject = chipData;
            modelNo = idx;
            switch (modelNo)
            {
                case 1:
                    {
                        this.Text = "CV1";
                    }
                    break;
                case 2:
                    {
                        this.Text = "CV2";
                    }
                    break;
                case 3:
                    {
                        this.Text = "检测模组1";
                    }
                    break;
                case 4:
                    {
                        this.Text = "检测模组2";
                    }
                    break;
                case 5:
                    {
                        this.Text = "检测模组3";
                    }
                    break;
                case 6:
                    {
                        this.Text = "检测模组4";
                    }
                    break;               
            }

            this.Show();
            this.timer1.Start();
            this.WindowState = FormWindowState.Normal;
            this.Activate();
        }

        int timecot = 0;
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (this.Visible == false) return;
            btnInspPass.Visible = false;

            btnDel.Monitor(false, ProcedureHelper_EQ.ManualMode);

            //读码失败，采用扫码
            btnSave.Visible = modelNo == 1 && ProcedureHelper_Exc.ExcSTCodeScanEnable;
            btnSave.BackColor = modelNo == 1 && ProcedureHelper_Exc.ExcSTCodeScanEnable && tbxWaferID.Text != "" && PulseOn.Pulse_1s ? Color.White : Color.Green;
            lblWaferIDRemind.Visible = modelNo == 1 && ProcedureHelper_Exc.ExcSTCodeScanEnable;
            tbxWaferID.Visible = modelNo == 1 && ProcedureHelper_Exc.ExcSTCodeScanEnable;

            if (!tbxWaferID.Focused)
            {
                tbxWaferID.Text = wfID;
            }

            if (timecot > 30)
            {
                propertyGrid1.Refresh();
                timecot = 0;
            }
            timecot++;
        }

        private void btn_Switch_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] info = btn.Tag.ToString().Split('|');
             
            switch (info[1])
            {
                case "Del":
                    {
                        chipData = new ChipInfo();
                        switch (modelNo)
                        {
                            case 1:
                                {
                                    ParameterHelper.ExcST = chipData;
                                    ProcedureHelper_Exc.SaveExcST();
                                }
                                break;
                            case 2:
                                {
                                    ParameterHelper.RBUpper = chipData;
                                    //ProcedureHelper_Efem.SaveRBUpper();
                                }
                                break;
                            case 3:
                                {
                                    ParameterHelper.RBLower = chipData;
                                    //ProcedureHelper_Efem.SaveRBLower();
                                }
                                break;
                            case 4:
                                {
                                    ParameterHelper.Aligner = chipData;
                                    //ProcedureHelper_Efem.SaveAligner();
                                }
                                break;
                            case 5:
                                {
                                    ParameterHelper.ExcTR = chipData;
                                    ProcedureHelper_Exc.SaveExcTR();
                                }
                                break;
                            case 6:
                                {
                                    ParameterHelper.SortST = chipData;
                                    ProcedureHelper_Exc.SaveSortST();
                                }
                                break;
                            case 7:
                                {
                                    ParameterHelper.SortSTNG = chipData;
                                    ProcedureHelper_Exc.SaveSortSTNG();
                                }
                                break;
                            case 8:
                                {
                                    ParameterHelper.NGPort1 = chipData;
                                    //ProcedureHelper_Efem.SaveNGPort1();
                                }
                                break;
                            case 9:
                                {
                                    ParameterHelper.NGPort2 = chipData;
                                    //ProcedureHelper_Efem.SaveNGPort2();
                                }
                                break;
                        }
                        this.Visible = false;
                    }
                    break;
                case "Save":
                    {
                        if (modelNo == 1 && ProcedureHelper_Exc.ExcSTCodeScanEnable)
                        {
                            if (wfID!=null&& wfID!="")
                            {
                                ParameterHelper.ExcST.Wafer_ID = wfID;
                                ProcedureHelper_Exc.SaveExcST();
                            }
                            tbxWaferID.Text = "";
                            wfID = "";
                        }
                        if (modelNo == 2)
                        {
                            //ParameterHelper.RBUpper.Wafer_ID = wfID;
                            //ProcedureHelper_Efem.SaveRBUpper();
                        }
                        if (modelNo == 7)
                        {
                            if (wfID != null && wfID != "")
                            {
                                ParameterHelper.SortSTNG.Wafer_ID = wfID;
                                ProcedureHelper_Exc.SaveSortSTNG();
                            }
                            tbxWaferID.Text = "";
                            wfID = "";
                        }
                    }
                    break;
                case "InspPass":
                    {
                      
                    }
                    break;
            }
        }

        private void FormChipData_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Visible = false;
            this.timer1.Stop();
            e.Cancel = true;
        }

        private void tbxWaferID_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                wfID = tbxWaferID.Text;
                ActiveControl = null;
            }
        }
    }
}
